﻿using System;
using System.Windows.Forms;
using PQ.Util;

namespace PQ
{
    public partial class Main : Form
    {
        ConfigForm cf;
        AccessUtil accessUtil = null;

        public Main()
        {
            InitializeComponent();
        }

        private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cf != null)
            {
                if (cf.isStart)
                {
                    MessageBox.Show("上次采样还没有停止，请先停止！");
                    return;
                }
                else
                    cf.Close();
            }
            cf = new ConfigForm();
            cf.MdiParent = this;
            cf.Show();
           
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cf != null)
            {
                cf.save_Click(sender,e);
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            new DataChart().Show();
            new Statistic().Show();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(DialogResult.OK == this.openFileDialog1.ShowDialog())
            {
                this.accessUtil = new AccessUtil(this.openFileDialog1.FileName);
                if (cf != null)
                {
                    cf.Close();
                }
                cf = new ConfigForm();
                cf.MdiParent = this;
                
            }
            
            this.openFileDialog1.Dispose();
        }

        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (cf == null)
            {
                return;
            }
            if (accessUtil != null)
                cf.showConfig(accessUtil.loadConfiguration());
            else
                cf.Show();
            
            
        }

        private void Main_DragEnter(object sender, DragEventArgs e)
        {
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void Main_DragDrop(object sender, DragEventArgs e)
        {
            Object obj = e.Data.GetData(DataFormats.FileDrop);
            Array files = obj as Array;
            foreach(object o in files)
            {
                string s = o as string;
                if (s != null)
                {
                    this.accessUtil = new AccessUtil(s);
                }
            }
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }
    }
}
